package model.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import DAO.HibernateUtil;
import DAO.ScheduleDAO;
import DAO.StationDAO;
import DAO.impl.ScheduleDAOImpl;
import DAO.impl.StationDAOImpl;
import DTO.DTOManager;
import DTO.StationDTO;

/**
 * Class that implements the model.Station interface.
 */
public class StationImpl implements model.Station {
	
	private StationDAO stationDAO = new StationDAOImpl();
	private ScheduleDAO scheduleDAO = new ScheduleDAOImpl();
	
	private Logger logger = LoggerFactory.getLogger("StationImpl");
	
	private DTOManager dtoManager = new DTOManager();
	
	/**
	 * This method checks if there is a station in DB which has the same 
	 * DTO implementation as the given DTO. If it is NOT found in DB, method 
	 * creates a new Station entity from DTO and saves it.
	 * 
	 * @param stationDTO - DTO of station which is required to be saved
	
	 */
	public void saveUniqueStation(StationDTO stationDTO) {
		try {
			HibernateUtil.beginTransaction();
			if (this.stationDAO.findStationByName(stationDTO
					.getStationName()) == null) {
				Station station = this.dtoManager
						.createNewStationFromDTO(stationDTO);
				this.stationDAO.save(station);
			}
			HibernateUtil.commitTransaction();
		} catch (HibernateException e) {
			this.logger.error("Error in save unique station " 
												+ e.getLocalizedMessage());
			HibernateUtil.rollbackTransaction();
		} 
	}
	
	/**
	 * This method checks if there is a station in DB which has the same 
	 * DTO implementation as given DTO. If it is found in DB, method 
	 * removes it.
	 * 
	 * @param stationDTO - DTO of station which is required to be removed
	
	 */
	public void removeUniqueStation(StationDTO stationDTO) {
		try {
			HibernateUtil.beginTransaction();
			Station station = this.stationDAO
					.findStationByName(stationDTO.getStationName());
			if (station != null) {
				List<Schedule> schedules = scheduleDAO
						.findAllSchedulesByStation(station);
				if (schedules != null) {
					for (int i = 0; i < schedules.size(); i++) {
						scheduleDAO.delete(schedules.get(i));
					}
				}
				this.stationDAO.delete(station);
			}
			HibernateUtil.commitTransaction();
		} catch (HibernateException e) {
			this.logger.error("Error in remove unique station " 
											+ e.getLocalizedMessage());
			HibernateUtil.rollbackTransaction();
		} 
	}

	/**
	 * Method looks for all the Station entities in DB.
	 * 
	 * @return	- List of all Station entities in DB or null if there is no
	 * 			  station in DB.
	 */
	@Override
	public List<model.impl.Station> findAll() {
		List<model.impl.Station> stations = null;
		try {
			HibernateUtil.beginTransaction();
			stations = this.stationDAO.findAll(model.impl.Station.class);
			HibernateUtil.commitTransaction();
		} catch (HibernateException e) {
			this.logger.error("Error in find all stations "
											+ e.getLocalizedMessage());
			HibernateUtil.rollbackTransaction();
		} 
		return stations;
	}
}
